/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

import java.awt.*;
import java.awt.event.*;
import java.awt.font.*;
import java.awt.geom.*;
import javax.swing.*;

public class JBanner extends JButton implements Animation {
	private int xpos = 0;
	private int currentx = 0;
	private int font_width = 0;

	public JBanner(String name) {
		super(name);
		setBorder(null);
		setFocusable(false);
		setDoubleBuffered(true);
	}

	public void addNotify() {
		super.addNotify();
		AnimationThread.add(this);
	}

	public void removeNotify() {
		super.removeNotify();
		AnimationThread.remove(this);
	}

	// Redraw
	public void paint(Graphics g1) {
		Graphics2D g = (Graphics2D)g1;
		if (isOpaque()) {
			g.setColor(getBackground());
			g.fillRect(0, 0, getWidth(), getHeight());
		}
		Font font = getFont();
		FontRenderContext frc = g.getFontRenderContext();
		Rectangle2D bounds = font.getStringBounds(getText(), frc);
		g.setFont(font);
		g.setColor(getForeground());
		font_width = (int)bounds.getWidth();
		int ypos = (int)(getHeight() - (getHeight() - bounds.getHeight()) / 2) - 3;
		g.drawString(getText(), xpos, ypos);
		g.drawString(getText(), xpos + (int)getSize().getWidth(), ypos);

		xpos--;
		if (xpos < -font_width) {
			xpos += getSize().getWidth();
		}
		if (xpos > currentx) {
			xpos--;
			if (xpos < -font_width) {
				xpos += getSize().getWidth();
			}
		}
	}

	public boolean isAnimating() {
		return true;
	}

	public int animationFPS() {
		return 30;
	}

	public void animate() {
		currentx -= 1;
		if (currentx < -font_width) {
			currentx += getSize().getWidth();
		}
		repaint();
	}
}

